@include('admin.header')
  <body>
    <div class="x-nav">
    <span class="layui-breadcrumb">
      <a href="/admin/welcome">首页</a>
        <a href="">友情链接管理</a>
        <a>
          <cite>友情链接列表</cite>
        </a>
      </span>
      <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:38px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input class="layui-input" placeholder="开始日" autocomplete="off" name="start" id="start">
          <input class="layui-input" placeholder="截止日" autocomplete="off" name="end" id="end">
          <input type="text" name="link_name"  placeholder="请输入链接名称" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button class="layui-btn" onclick="x_admin_show('添加链接','/admin/link_add')"><i class="layui-icon"></i>添加</button>
        <span class="x-right count" style="line-height:40px"></span>
      </xblock>
      <div class="list">




      </div>
      <div class="page">

      </div>

    </div>
    <div class="alert">
      提示信息
    </div>
    <style>
      .alert{
        position: fixed;
        width: 280px;
        height: 50px;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        z-index: 9999;
        color: #fff;
        text-align: center;
        line-height: 50px;
        font-size: 16px;
        display: none;
      }
    </style>
    <script>
      layui.use('laydate', function() {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });
        layui.use('form', function () {
          var form = layui.form;
          form.on('submit(sreach)', function (data) {
            //getGoodData(page);
            var admin = data.field;
            var str = '';
            for (let k in admin) {
              str += '&' + k + '=' + admin[k];
            }
            $.ajax({
              type: "get",
              url: "{{url('/admin/toget_link_list')}}",
              data: "page=1" + str,
              dataType: "html",
              success: function (res) {
                $(".list").html(res);
                resetpage();
                $.ajax({
                  type: "get",
                  url: "{{url('/admin/toget_link_count')}}",
                  data: "page=1" + str,
                  dataType: "text",
                  success: function (res) {
                    $(".count").html("共有管理员：" + res + " 人");
                  }
                })
              }
            })
            return false;
          })
          //搜索功能
          form.on("submit(sreach)", function (data) {
            console.log(data.field);
            var data_obj = data.field;
            if (data_obj["start"] != "") {
              start = "&start=" + data_obj["start"];
            } else {
              start = "";
            }
            if (data_obj["end"] != "") {
              end = "&end=" + data_obj["end"];
            } else {
              end = "";
            }

            if (data_obj["link_name"] != "") {
              linkName = "&link_name=" + data_obj["link_name"];
            } else {
              linkName = "";
            }
            var getstr = searchItem();
            $.ajax({
              type: "get",
              url: "{{url('/admin/toget_link_list')}}",
              data: "page=1" + getstr,
              dataType: "html",
              success: function (res) {
                $(".list").html(res);
                resetpage();
                getCount(getstr);
                form.render();
              }
            })
            return false;
          })
        })


        /*链接-删除*/
        function member_del(obj, id) {
          layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
          //$(obj).parents("tr").remove();
          //layer.msg('已删除!',{icon:1,time:1000});
          $.ajax({
            type:"get",
            url:"{{url('/admin/todo_link_del')}}",
            data:"link_id="+id,
            dataType:"json",
            success:function(res){
              console.log(res);
              if(res.code==1){
                layer.msg('已删除!',{icon:1,time:1000});
                getGoodData(1);
                getCount();
              }else {
                layer.msg('数据执行有误!',{icon:1,time:1000});
              }
            }
          })
        });
        }

        var linkName = "";
        var classify_id = "";
        var start = "";
        var end = "";

        function searchItem() {
          var str = "";
          if (linkName != "") {
            str += linkName;
          }
          if (classify_id != "") {
            str += classify_id;
          }
          if (start != "") {
            str += start;
          }
          if (end != "") {
            str += end;
          }
          return str;
        }


        function delAll(argument) {

          var data = tableCheck.getData();

          layer.confirm('确认要删除吗？' + data, function (index) {
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
          });
        }

    </script>
    <script>
      //全选功能
      $(document).on("change","input[name='all']",function () {
        var flag = $("input[name='all']").get(0).checked;
        var arr = $("input[name='id[]']");
        for (let i = 0; i < arr.length; i++) {
          arr.get(i).checked = flag;
        }
      })
      //重置分页的样式
      function resetpage(){
        //把span标签改成a标签
        var spans = $('.page-item span');
        for (let i = 0; i < spans.length; i++) {
          spans[i].outerHTML="<a href=''>"+spans[i].innerHTML+"</a>";
        }
        var arr = $(".page-item a");
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].innerHTML=="‹") {
            arr[i].className = "prev";
          }else if(arr[i].innerHTML=="›"){
            arr[i].className = "next";
          }
        }
        //将当前页设置为active
        $(".active a").attr("class","num active");
        //把li 上面的active 去除
        $("li.active").removeClass("active");
        //给页码a标签换类名
        $(".page-item .page-link").attr("class","num");
      }


      //请求接口获取某一页的数据
      function getGoodData(page,getstr=""){
        $.ajax({
          type:"get",
          url:"{{url('/admin/toget_link_list')}}",
          data:"page="+page+getstr,
          dataType:"html",
          success:function(res){
            $(".list").html(res);
            resetpage();
          }
        })
      }
      //请求接口获取所有数据的条数
      function getCount(getstr=""){
        $.ajax({
          type:"get",
          url:"{{url('/admin/toget_link_count')}}",
          data:"p=1"+getstr,
          dataType:"text",
          success:function(res){
            $(".count").html("共有链接："+res+" 条");
          }
        })
      }
      //页面加载完要先请求第一页和第一次请求数据总数
      getGoodData(1);
      getCount();
      //点击页码请求接口
      $(document).on("click",".num",function () {
        var page = $(this).html();
        let condition = searchItem();
        getGoodData(page,condition);
        return false;
      })
      //点击上一页请求接口
      $(document).on("click",".prev",function () {
        var isdisable = $(this).parent(".disabled").length;
        var cur = $(".page-item").find(".active").html();
        if (isdisable) {
          $(".alert").html("这是梦开始的地方").css({"background-color":"#41b314bb","border-color":"#3ca512bb"}).stop().fadeIn(500);
          setTimeout(function () {
            $(".alert").fadeOut(500);
          },2000)
        }else{
          var page = parseInt(cur)-1
          let condition = searchItem();
          getGoodData(page,condition);
        }
        return false;
      })
      //点击下一页请求接口
      $(document).on("click",".next",function () {
        var isdisable = $(this).parent(".disabled").length;
        var cur = $(".page-item").find(".active").html();
        if (isdisable) {
          $(".alert").html("这里的故事等你来续写").css({"background-color":"#41b314bb","border-color":"#3ca512bb"}).stop().fadeIn(500);
          setTimeout(function () {
            $(".alert").fadeOut(500);
          },2000)
        }else{
          var page = parseInt(cur)+1
         let condition = searchItem();
          getGoodData(page,condition);
        }
        return false;
      })
    </script>

  </body>

</html>